import pointGoodsApi from '../../api/pointGoodsApi'
Page({

    /**
     * 页面的初始数据
     */
    data: {
      pageNum: 0,
      pageSize: 10,
      list: [],
      isMore: true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      this.getList(1)
    },

    async getList(num){
      if(!this.data.isMore){
        return
      }
      this.data.pageNum = num
      let res = await pointGoodsApi.getList({
        pageNum: this.data.pageNum,
        pageSize: this.data.pageSize
      })
      
      this.setData({
        list: this.data.list.concat(res),
        isMore: res.length == this.data.pageSize
      })
    },


    toUpdate(e){  
      let item = e.currentTarget.dataset.item
      console.log(item)
      wx.navigateTo({
        url: '/pointShop/addPointGoods/addPointGoods?_id=' + item._id,
      })
    },
    async toDelete(e){
      let item = e.currentTarget.dataset.item
      let index = e.currentTarget.dataset.index
      wx.showModal({
        title: '提示',
        content: '确定删除？',
        success: async (res) =>  {
          if (res.confirm) {
            wx.showLoading({
              mask: true
            })
            await pointGoodsApi.delete(item._id)
           
            // wx.cloud.deleteFile({
            //   fileList: item.fileList
            // })
          
            this.data.list.splice(index, 1)
            this.setData({
              list: this.data.list
            })
            wx.hideLoading()
          } 
        }
      })
      
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    async onPullDownRefresh() {
      this.setData({
        list: [],
        isMore: true
      })
      await this.getList(1)
      wx.stopPullDownRefresh()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
      this.getList(this.data.pageNum + 1)
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})